package com.example.clientmodule;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.example.servicemodule.Book;
import com.example.servicemodule.IBookManager;
import com.example.servicemodule.OnInfoChangeAidlInterface;
import com.example.servicemodule.ServiceAidlInterface;
import com.example.servicemodule.Student;
import com.example.servicemodule.ViewInfo;

public class MainActivity extends Activity implements View.OnClickListener {
    private ServiceAidlInterface binder;

    private static final String TAG = "MainActivity";

    //默认未绑定service
    private boolean isBind = false;

    private ViewGroup mViewGroup;

    private Bitmap mBitmap;

    private OnInfoChangeAidlInterface mOnInfoChangeAidlInterface = new OnInfoChangeAidlInterface.Stub() {
        @Override
        public void onNewInfo(Student student) throws RemoteException {
            Log.i(TAG, "onNewInfo: student registener listener is:" + student.toString());
        }
    };

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: " + name);
            binder = ServiceAidlInterface.Stub.asInterface(service);
            try {
                Log.i(TAG, "onServiceConnected: mOnInfoChangeAidlInterface" + mOnInfoChangeAidlInterface);
                binder.registerListener(mOnInfoChangeAidlInterface);
                Log.i(TAG, "onServiceConnected: registeer end");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };

    private IBookManager mBookManager;

    private ServiceConnection mBookServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBookManager = IBookManager.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private Button bind_btn;

    private Button unBind_btn;

    private Button get_str_info;

    private Button get_stu_info;

    private Button recycle_bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewGroup = findViewById(R.id.layout);
        // bind service
        bind_btn = findViewById(R.id.bind_service);
        unBind_btn = findViewById(R.id.unbind_service);
        get_str_info = findViewById(R.id.get_str_info);
        get_stu_info = findViewById(R.id.get_stu_info);
        recycle_bitmap = findViewById(R.id.recycle_bitmap);
        bind_btn.setOnClickListener(this);
        unBind_btn.setOnClickListener(this);
        get_str_info.setOnClickListener(this);
        get_stu_info.setOnClickListener(this);
        recycle_bitmap.setOnClickListener(this);
        findViewById(R.id.bindBoookService).setOnClickListener(this);
        findViewById(R.id.book_in).setOnClickListener(this);
        findViewById(R.id.book_out).setOnClickListener(this);
        findViewById(R.id.book_in_out).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bind_service:
                if (conn != null) {
                    Intent intent = new Intent("com.example.aidlservice.remote");
                    intent.setPackage("com.example.servicemodule");
                    bindService(intent, conn, Context.BIND_AUTO_CREATE);
                    isBind = true;
                } else {
                    Toast.makeText(this, "bind service", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.unbind_service:
                if (isBind) {
                    if (conn != null) {
                        isBind = false;
                        Toast.makeText(this, "unbind service conn", Toast.LENGTH_SHORT).show();
                        unbindService(conn);
                    }
                }
                break;
            case R.id.get_str_info:
                Log.d(TAG, "get_str_info " + conn);
                if (binder == null) {
                    Toast.makeText(this, "binder is null", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        Toast.makeText(this, " " + binder.getInfo(), Toast.LENGTH_SHORT).show();
                        ViewInfo viewInfo = binder.getViewInfo();
                        Log.i(TAG, "onClick: viewInfo.name is " + viewInfo.getName());
                        View view1 = viewInfo.getRemoteViews().apply(this, mViewGroup);
                        mViewGroup.addView(view1);
                        ImageView imageView = findViewById(R.id.showBitmap);
                        mBitmap = binder.getBitmap();
                        imageView.setImageBitmap(mBitmap);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.get_stu_info:
                if (binder == null) {
                    Toast.makeText(this, "binder is null", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        Toast.makeText(this, " " + binder.getStudentInfo(), Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            case R.id.recycle_bitmap:
                Log.i(TAG, "onClick: bitmap" + mBitmap);
                mBitmap.recycle();
                Log.i(TAG, "onClick: mBitmap" + mBitmap.isRecycled());
            case R.id.bindBoookService:
                Log.i(TAG, "onClick: bindBookService");
                Intent intent = new Intent("com.example.aidlservice.bookManagerService");
                intent.setPackage("com.example.servicemodule");
                bindService(intent, mBookServiceConnection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.book_in:
                StringBuilder stringBuilder = new StringBuilder();
                Book sourece = new Book(123, "liuxudong_test_in");
                stringBuilder.append(sourece.toString()).append('\n');
                try {
                    String result = mBookManager.addInBook(sourece).toString();
                    stringBuilder.append(result).append('\n');
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                stringBuilder.append(sourece);
                Log.i(TAG, "onClick: in test" + stringBuilder.toString());
                break;
            case R.id.book_out:
                StringBuilder stringBuilderOutTest = new StringBuilder();
                Book outSourceTest = new Book(456, "liuxudong_test_out");
                stringBuilderOutTest.append(outSourceTest).append('\n');
                try {
                    String result = mBookManager.addOutBook(outSourceTest).toString();
                    stringBuilderOutTest.append(result).append('\n');
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                stringBuilderOutTest.append(outSourceTest);
                Log.i(TAG, "onClick: stringBuilderOutTest" + stringBuilderOutTest.toString());
            case R.id.book_in_out:
                StringBuilder stringBuilderOutInTest = new StringBuilder();
                Book outInSourceTest = new Book(789, "liuxudong_test_in_out");
                stringBuilderOutInTest.append(outInSourceTest).append('\n');
                try {
                    String result = mBookManager.addInoutBook(outInSourceTest).toString();
                    stringBuilderOutInTest.append(result).append('\n');
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                stringBuilderOutInTest.append(outInSourceTest);
                Log.i(TAG, "onClick: stringBuilderOutInTest" + stringBuilderOutInTest);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            binder.unregisterListener(mOnInfoChangeAidlInterface);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (isBind) {
            if (conn != null) {
                isBind = false;
                unbindService(conn);
            }
        }
    }
}
